import Vue from 'vue'

import Vuex from 'vuex'
import nest from "../websocket/stores/nest";
import commandStatus from "../websocket/stores/command";
import {
	getNestInfoList
} from "../api/index.js"
import smartFlight from "./smartFlight";

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {

		commandNotificationWaitQueue: [],

		// 无人机
		mechineEvent: {
			id: 0,
			name: "013-百俐测试御3",
			height: 3.0,
			distance: 14.3,
			signal: 4,
			rtk: true,
			satelliteNumber: 77,
			electric: 65
		},
		// 机巢列表
		machineList: [],
		commandNotificationQueue: []
	},
	mutations: {
		// 修改无人机直播控制按钮（模拟）
		changeControlIconList(state, value) {
			let index = state.controlIconList.findIndex(item => item.id === value)
			state.controlIconList[index].check = !state.controlIconList[index].check
		},
		// 切换无人机巢
		changeMechineEvent(state, value) {
			state.mechineEvent.id = value.id
			state.mechineEvent.name = value.name
			state.mechineEvent.nestId = value.nestId
		},
		// // 修改无人机控制按钮状态
		// changeDroneControlList(state, value) {
		// 	let index = state.droneControlList.findIndex(item => item.id === value)
		// 	state.droneControlList[index].check = !state.droneControlList[index].check
		// },
		// // 修改机巢控制按钮状态
		// changeMachineControlList(state, value) {
		// 	let index = state.machineControlList.findIndex(item => item.id === value)
		// 	state.machineControlList[index].check = !state.machineControlList[index].check
		// },
		// 初始化机巢列表及初始化默认机巢
		changeMachineList(state, value) {
			state.machineList = value
			state.mechineEvent.name = value[0].nestName
			state.mechineEvent.id = value[0].ID
			state.mechineEvent.nestId = value[0].nestid
			console.log(state.mechineEvent.nestId, 'state.mechineEvent.nestId')
		},
		pushCmdToNotifyWaitQueue(state, payload) {
			state.commandNotificationWaitQueue.push(payload);
		},
		pushCmdToNotifyQueue(state, payload) {
			state.commandNotificationQueue.push(payload);
		},
		deleteCmdNotifyQueueWaitByCode(state, payload) {
			state.commandNotificationWaitQueue = state.commandNotificationWaitQueue.filter(
				(q) => q.commandCode !== payload.code
			);
		},
	},
	actions: {
		// 获取机巢列表信息
		initMachineList(state) {
			getNestInfoList().then(res => {
				const {
					data: {
						data: {
							list
						}
					}
				} = res
				console.log(list, 'action')
				state.commit('changeMachineList', list)
			})
		}
	},
	modules: {
		nest,
		commandStatus,
		smartFlight
	}
})

export default store